package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.SchOrg;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.SchOrgService;
import nsu.edu.zsq.util.UploadFileUtil;

/** 
* Description: SchMajor的controller类<br>
*/
@Controller
@RequestMapping("/sys/school/major")
public class SchMajorController {
    
    @Autowired
    private SchOrgService schOrgService;
    @Autowired
    private SchMajorService schMajorService;
    
    /**
     * 获取学校院系的树结构数据以及院系列表
     * @return JsonData
     */
    @RequestMapping("/listSchOrgWithTree")
    @ResponseBody
    public JsonData listSchOrgWithTree() {
        String listSchOrgWithTree = schOrgService.listSchOrgParamWithTree();
        List<SchOrg> listSchOrg = schOrgService.listSchOrg();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithTree", listSchOrgWithTree);
        map.put("listSchOrg", listSchOrg);
        return JsonData.success(map);
    }
    
    /**
     * 通过院系id获取专业的分页数据
     * @param orgId 院系id
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchMajor")
    @ResponseBody
    public JsonData listSchMajor(@RequestParam("orgId") int orgId, PageQuery pageQuery) {
        PageInfo<SchMajor> listSchMajor = schMajorService.listSchMajorWithPage(orgId, pageQuery);
        return JsonData.success(listSchMajor);
    }
    
    /**
     * 保存一条专业信息
     * @param schMajor
     * @return JsonData
     */
    @RequestMapping("/saveSchMajor")
    @ResponseBody
    public JsonData saveSchMajor(SchMajor schMajor) {
        schMajorService.saveSchMajor(schMajor);
        return JsonData.success();
    }
    
    /**
     * 修改专业信息
     * @param schMajor 专业信息
     * @return JsonData
     */
    @RequestMapping("/updateSchMajor")
    @ResponseBody
    public JsonData updateSchMajor(SchMajor schMajor) {
        schMajorService.updateSchMajor(schMajor);
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除专业
     * @param majorId 专业id数组
     * @return JsonData
     */
    @RequestMapping("/delSchMajor")
    @ResponseBody
    public JsonData delSchMajor(@RequestParam("majorId[]")int majorId[]) {
        if (majorId.length > 1) {
            // 批量删除
            schMajorService.deleteBatchSchMajor(majorId);
        } else if (majorId.length == 1) {
            // 单个删除
            schMajorService.deleteSchMajor(majorId[0]);
        } else {
            throw new ParamException("majorId无参数");
        }
        return JsonData.success();
    }
    
    
}
